import logging

from notifications.models import Notification
from rest_framework import viewsets, status
from rest_framework.generics import CreateAPIView
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.viewsets import ViewSet
from django.core.exceptions import ObjectDoesNotExist, SuspiciousOperation
from backend.permissions import IsStaffAuthenticated
from notice.serializers import NotificationSerializer, SendToAllUserSerializer


def CheckNoticePermission(notification_id: int, user):
    try:
        notification_obj = Notification.objects.get(id=notification_id)
    except ObjectDoesNotExist:
        return Response(status=status.HTTP_404_NOT_FOUND)
    if notification_obj.recipient_id != user.id:
        raise SuspiciousOperation("请停止你的行为，你的行为已经记录并通知管理员")
    return notification_obj


class UnreadNotificationsList(ViewSet):
    serializer_class = NotificationSerializer
    permission_classes = [IsAuthenticated]

    def list(self, request, *args, **kwargs):
        queryset = Notification.objects.filter(recipient_id=request.user.id, unread=True)
        return Response(NotificationSerializer(queryset, many=True).data)


class MarkAllAsRead(APIView):
    serializer_class = NotificationSerializer
    permission_classes = [IsAuthenticated]

    def get(self, request, *args, **kwargs):
        queryset = Notification.objects.filter(recipient_id=request.user.id, unread=True)
        queryset.update(unread=False)
        return Response(status=status.HTTP_200_OK)


class MarkAsRead(APIView):
    serializer_class = NotificationSerializer
    permission_classes = [IsAuthenticated]

    def get(self, request, *args, **kwargs):
        notification_id = kwargs.get('notification_id')
        notification_obj = CheckNoticePermission(notification_id, request.user)
        notification_obj.unread = False
        notification_obj.save()
        return Response(status=status.HTTP_200_OK)


class MarkAsUnread(APIView):
    serializer_class = NotificationSerializer
    permission_classes = [IsAuthenticated]

    def get(self, request, *args, **kwargs):
        notification_id = kwargs.get('notification_id')
        notification_obj = CheckNoticePermission(notification_id, request.user)
        notification_obj.unread = True
        notification_obj.save()
        return Response(status=status.HTTP_200_OK)


class NoticeDelete(APIView):
    serializer_class = NotificationSerializer
    permission_classes = [IsAuthenticated]

    def get(self, request, *args, **kwargs):
        return Response(status=status.HTTP_403_FORBIDDEN)

    def post(self, request, *args, **kwargs):
        return Response(status=status.HTTP_403_FORBIDDEN)

    def put(self, request, *args, **kwargs):
        return Response(status=status.HTTP_403_FORBIDDEN)

    def delete(self, request, *args, **kwargs):
        print(kwargs)
        notification_id = kwargs.get('notification_id')
        print(notification_id)
        notification_obj = CheckNoticePermission(notification_id, request.user)
        notification_obj.delete()
        return Response(status=status.HTTP_200_OK)


class AddNotification(CreateAPIView):
    serializer_class = NotificationSerializer
    permission_classes = [IsAuthenticated]

    def create(self, request, *args, **kwargs):
        response = super(AddNotification, self).create(request, *args, **kwargs)
        return response


class AllNotification(ViewSet):
    serializer_class = NotificationSerializer
    permission_classes = [IsAuthenticated]

    def list(self, request, *args, **kwargs):
        queryset = Notification.objects.filter(recipient_id=request.user.id)
        return Response(NotificationSerializer(queryset, many=True).data)


class UnreadNotificationCount(APIView):
    serializer_class = NotificationSerializer
    permission_classes = [IsAuthenticated]

    def get(self, request, *args, **kwargs):
        queryset = Notification.objects.filter(recipient_id=request.user.id, unread=True)
        count = queryset.count()
        data = {
            'unread_count': count
        }
        return Response(data, status=status.HTTP_200_OK)


class AllNotificationCount(APIView):
    serializer_class = NotificationSerializer
    permission_classes = [IsAuthenticated]

    def get(self, request, *args, **kwargs):
        queryset = Notification.objects.filter(recipient_id=request.user.id)
        count = queryset.count()
        data = {
            'all_count': count
        }
        return Response(data, status=status.HTTP_200_OK)


class NotificationViewSet(viewsets.ModelViewSet):
    serializer_class = NotificationSerializer
    queryset = Notification.objects.all()


class NotificationForAllViewSet(CreateAPIView):
    serializer_class = SendToAllUserSerializer
    permission_classes = [IsStaffAuthenticated]

    def create(self, request, *args, **kwargs):
        super(NotificationForAllViewSet, self).create(request, *args, **kwargs)
        return Response(status=status.HTTP_200_OK)
